//
//  MonitorViewController.m
//  CityFire
//
//  Created by Liuyz on 2017/7/31.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "MonitorViewController.h"
#import<AudioToolbox/AudioToolbox.h>
#import "MonitorViewModel.h"
#import "DeviceItemTableViewCell.h"
#import "DeviceDetailViewController2.h"
#import "MonitorRealTableViewCell.h"
#import "MonitorRealTableViewCell2.h"
#import "DeviceManagerTableViewCell.h"
#import "QueryHistoryViewController.h"
#import "MonitorItemTableViewCell.h"
#import "MoreResultViewController.h"

#import "WSDatePickerView.h"
#import "DeviceViewModel.h"

#import "DeviceDetailInfoViewController.h"
#import "HistoryDetaileViewController.h"
#import "SegmentView.h"



static SystemSoundID shake_sound_male_id = 0;

typedef NS_ENUM(NSUInteger, MonitorType) {
    MonitorTypeReal = 0x111111,
    MonitorTypeHistory,
    MonitorTypeDevice,
};


@interface MonitorViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) MonitorViewModel *monitorViewModel;
@property (strong, nonatomic) DeviceViewModel *deviceViewModel;

//批处理
@property (weak, nonatomic) IBOutlet UIButton *moreResultButton;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, weak) IBOutlet UISegmentedControl *setmentedControl;
@property (nonatomic, assign) MonitorType type;

@property (nonatomic, weak) IBOutlet UIView      *historyView;
@property (nonatomic, weak) IBOutlet UILabel     *startTimeLabel;
@property (nonatomic, weak) IBOutlet UILabel     *endTimeLabel;
@property (nonatomic, weak) IBOutlet UIButton    *startBtn;
@property (nonatomic, weak) IBOutlet UIButton    *endBtn;
@property (nonatomic, weak) IBOutlet UIButton    *serarchBtn;
@property (nonatomic, weak) IBOutlet UIButton    *oneDayBtn;
@property (nonatomic, weak) IBOutlet UIButton    *sevenDayBtn;
@property (nonatomic, weak) IBOutlet UISearchBar *historySearchBar;

@property (nonatomic, weak) IBOutlet SegmentView *historySegmentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *historyConstraintHeight;
@property (nonatomic, weak) IBOutlet SegmentView *mydeviceSegmentView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mydeviceConstraintHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewTopConstraintHeight;

@property (nonatomic, strong) NSMutableArray *newDeviceArray;
@property (nonatomic, strong) NSMutableArray *devicesArray;

@property (nonatomic, assign) NSInteger selectedHistoryIndex;
@property (nonatomic, assign) NSInteger selectedmyDeviceIndex;
@property(nonatomic,assign) BOOL isShowMoreResult;
@property(nonatomic,assign) BOOL isLoadingData;

@end

@implementation MonitorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    self.title = kMonitor_TITLE;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MonitorItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"MonitorItemTableViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DeviceItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"DeviceItemTableViewCell"];
    
    self.isLoadingData = NO;
    self.isShowMoreResult=NO;
    self.refreshScrollView = self.tableView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor redColor];
    self.isAutoRefresh = YES;
    
    [self.moreResultButton setTitle:@"批处理" forState:UIControlStateNormal];
    [self.moreResultButton setTitle:@"选择处理" forState:UIControlStateSelected];
    
    @weakify(self);
    self.refreshActionBlock = ^(){
        @strongify(self);
        if (self.isLoadingData) {
            return ;
        }
        self.isLoadingData = YES;
        //获取实时接口
        switch (self.type) {
            case MonitorTypeDevice:
                //设备
                [self.monitorViewModel getAllDeviceInfo:@{@"pageNo" : @"1", @"pageSize" : @"500"}];
                
                break;
            case MonitorTypeHistory:
                //历史
                
                break;
            case MonitorTypeReal:
                //实时
                [self.monitorViewModel getQueryRealTimeWarning:nil];
                [self.monitorViewModel getQueryRealTimeWarningCount:nil];
                
                break;
                
            default:
                break;
        }
        [self.monitorViewModel getQueryRealTimeWarningCount:nil];
    };
    
    self.loadMoreActionBlock = ^(){
        @strongify(self);
        if (self.isLoadingData) {
            return ;
        }
        self.isLoadingData = YES;
        switch (self.type) {
            case MonitorTypeDevice:
                //设备
                [self.monitorViewModel getAllDeviceInfo:@{@"pageNo" : @"1", @"pageSize" : @"500"}];
                
                break;
            case MonitorTypeHistory:
                //历史
                
                break;
            case MonitorTypeReal:
                //实时
                [self.monitorViewModel getQueryRealTimeWarningCount:nil];
                
                break;
                
            default:
                break;
        }
        
        //        [self getMessageList];
    };
    [self.tableView reloadData];
    self.tableView.tableFooterView = [UIView new];
    
    [self bindRAC];
    
    self.type = MonitorTypeReal;
    
    
    //    [self.monitorViewModel getQueryRealTimeWarningCount:nil];
    
    //    if (YES) {
    //        self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", 100];
    //    }else{
    //        self.navigationController.tabBarItem.badgeValue = nil;
    //    }
    self.historyConstraintHeight.constant = 0;
    self.mydeviceConstraintHeight.constant = 0;
    self.tableViewTopConstraintHeight.constant  = 0;
    
    [self configUI];
    
}


-(void) playSound :(NSInteger ) index{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"6" ofType:@"mp3"];
    if (index==3) {
        //报警
        path = [[NSBundle mainBundle] pathForResource:@"6" ofType:@"mp3"];
    } else {
        //故障
        path = [[NSBundle mainBundle] pathForResource:@"7" ofType:@"mp3"];
    }
    
    
    if (path) {
        //注册声音到系统
        
        AudioServicesCreateSystemSoundID(((__bridge CFURLRef)[NSURL fileURLWithPath:path]),&shake_sound_male_id);
        
        BOOL isPlay = [[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_ISSOUND] boolValue];
        BOOL isVip =[[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_VIBRATE] boolValue];
        if (isPlay) {
             AudioServicesPlaySystemSound(shake_sound_male_id);
        }
        if (isVip) {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        }
       
        //        AudioServicesPlaySystemSound(shake_sound_male_id);//如果无法再下面播放，可以尝试在此播放
    }
    
   
    
    //    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   //让手机震动
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"***************%@ count:%ld",self.newDeviceArray,self.newDeviceArray.count);
    //    [self.monitorViewModel getQueryRealTimeWarning:nil];
    self.refreshActionBlock();
    //    [self bindRAC];
    //    [self.tableView reloadData];
    if (self.isLoadingData) {
        return ;
    }
    self.isLoadingData = YES;
    [self.devicesArray removeAllObjects];
    [self.newDeviceArray removeAllObjects];
    [self.tableView reloadData];
    switch (self.type) {
        case MonitorTypeDevice:
            //设备
            [self.monitorViewModel getAllDeviceInfo:@{@"pageNo" : @"1", @"pageSize" : @"500"}];
            
            break;
        case MonitorTypeHistory:
            //历史
            
            break;
        case MonitorTypeReal:
            //实时
            [self.monitorViewModel getQueryRealTimeWarningCount:nil];
            [self.monitorViewModel getQueryRealTimeWarning:nil];
            break;
            
        default:
            break;
    }
    //[self.monitorViewModel getQueryRealTimeWarningCount:nil];
    
    
    
}
- (void)configUI
{
    
    
    
    
    self.historyView.hidden = YES;
    self.startTimeLabel.text = [[NSDate date]  stringWithFormat:@"yyyyMMdd"];
    self.endTimeLabel.text = [[NSDate date]  stringWithFormat:@"yyyyMMdd"];
    [self.historySegmentView setLineColor:[UIColor colorWithRed: 5 / 255.0 green: 160 / 255.0 blue: 239 / 255.0 alpha:1.0]];
    [self.historySegmentView setTitleHighlightedColor:[UIColor colorWithRed: 5 / 255.0 green: 160 / 255.0 blue: 239 / 255.0 alpha:1.0]];
    [self.historySegmentView setTitleNormalColor:[UIColor colorWithRed: 101 / 255.0 green: 102 / 255.0 blue: 103 / 255.0 alpha:1.0]];
    
    [self.historySegmentView setTitleArray:@[@"报警", @"故障"]];
    [self.historySegmentView segmentBlock:^(NSInteger index) {
        NSLog(@"history 点击了%ld",index);
        self.selectedHistoryIndex = index;
        [self.monitorViewModel  getQueryHistoryWarning:@{@"start_date" : self.startTimeLabel.text, @"end_date" : self.endTimeLabel.text, @"type" : self.selectedHistoryIndex == 0 ? @"报警" : @"故障"}];
    }];
    
    [self.mydeviceSegmentView setLineColor:[UIColor colorWithRed: 5 / 255.0 green: 160 / 255.0 blue: 239 / 255.0 alpha:1.0]];
    [self.mydeviceSegmentView setTitleHighlightedColor:[UIColor colorWithRed: 5 / 255.0 green: 160 / 255.0 blue: 239 / 255.0 alpha:1.0]];
    [self.mydeviceSegmentView setTitleNormalColor:[UIColor colorWithRed: 101 / 255.0 green: 102 / 255.0 blue: 103 / 255.0 alpha:1.0]];
    [self.mydeviceSegmentView setTitleArray: @[@"正常",@"故障", @"报警",  @"异常", @"离线"]];
    
    [self.mydeviceSegmentView segmentBlock:^(NSInteger index) {
        self.selectedmyDeviceIndex = index;
        [self.newDeviceArray removeAllObjects];
        for (MonitorModel *model in self.devicesArray) {
            switch (self.selectedmyDeviceIndex) {
                case 0:
                    if ([model.status1 isEqualToString:@"01"]) {
                        [self.newDeviceArray addObject:model];
                    }
                    break;
                case 1:
                    if ([model.status1 isEqualToString:@"02"]) {
                        [self.newDeviceArray addObject:model];
                    }
                    
                    break;
                case 2:
                    if ([model.status1 isEqualToString:@"03"]) {
                        [self.newDeviceArray addObject:model];
                    }
                    break;
                case 3:
                    if ([model.status1 isEqualToString:@"04"]) {
                        [self.newDeviceArray addObject:model];
                    }
                    break;
                case 4:
                    if ([model.status1 isEqualToString:@"05"]) {
                        [self.newDeviceArray addObject:model];
                    }
                    break;
                default:
                    break;
            }
        }
        [self.tableView  reloadData];
    }];
    
    
    self.moreResultButton.hidden = YES;
    self.type = MonitorTypeHistory;
    self.historyView.hidden = NO;
    self.mydeviceSegmentView.hidden = YES;
    self.historySegmentView.hidden = NO;
    self.historyConstraintHeight.constant = 25;
    self.mydeviceConstraintHeight.constant = 0;
    self.tableViewTopConstraintHeight.constant = 25;
    [self.tableView  reloadData];
}

/**
 绑定数据源
 */
- (void)bindRAC
{
    [RACObserve(self.monitorViewModel, requestError) subscribeNext:^(NSError *requestError) {
        self.isLoadingData = NO;
        [self endRefresh];
    }];
    
    [RACObserve(self.monitorViewModel, responseInfo) subscribeNext:^(NSError *requestError) {
        [self endRefresh];
        self.isLoadingData = NO;
    }];
    
    [RACObserve(self.monitorViewModel, monitorArray) subscribeNext:^(NSArray *mArray) {
        [self endRefresh];
        self.isLoadingData = NO;
        if (mArray.count) {
            if (self.type == MonitorTypeDevice) {
                self.devicesArray = [mArray copy];
                [self.newDeviceArray removeAllObjects];
                for (MonitorModel *model in mArray) {
                    switch (self.selectedmyDeviceIndex) {
                        case 0:
                            if ([model.status1 isEqualToString:@"01"]) {
                                [self.newDeviceArray addObject:model];
                            }
                            break;
                        case 1:
                            if ([model.status1 isEqualToString:@"02"]) {
                                [self.newDeviceArray addObject:model];
                            }
                            
                            break;
                        case 2:
                            if ([model.status1 isEqualToString:@"03"]) {
                                [self.newDeviceArray addObject:model];
                            }
                            break;
                        case 3:
                            if ([model.status1 isEqualToString:@"04"]) {
                                [self.newDeviceArray addObject:model];
                            }
                            break;
                        case 4:
                            if ([model.status1 isEqualToString:@"05"]) {
                                [self.newDeviceArray addObject:model];
                            }
                            break;
                        default:
                            break;
                    }
                }
            }else if (self.type == MonitorTypeReal) {
                
                [self setupUnreadCount:mArray.count];
                
                
                
                NSInteger isPlay =0;
                 for (MonitorModel *model in mArray) {
                     NSString *status = model.record2;
                     //报警
                     if ([status isEqualToString:@"报警"]) {
                         isPlay =3;
                     }
                 }
            
                if (isPlay==3) {
                    //播放报警
                    [self playSound:3];
                }else{
                    for (MonitorModel *model in mArray) {
                        NSString *status = model.record2;
                        //报警
                        if ([status isEqualToString:@"故障"]) {
                            isPlay =2;
                        }
                    }
                    if (isPlay==2) {
                        //故障
                        [self playSound:2];
                    }
                }
            }
            [self.tableView reloadData];
        }else if (mArray) {
            [SVProgressHUD showInfoWithStatus:@"没有数据～～"];
            [self.tableView reloadData];
        }
    }];
    
    [RACObserve(self.monitorViewModel, countModel) subscribeNext:^(MonitorCountModel *countModel) {
        [self endRefresh];
        
        if ([countModel.waringSize integerValue]) {
            self.navigationController.tabBarItem.badgeValue = [countModel.waringSize stringValue];
        }else {
            self.navigationController.tabBarItem.badgeValue = nil;
        }
    }];
}
//批处理点击事件
- (IBAction)moreResultClick:(id)sender {
    if (self.isShowMoreResult) {
        self.isShowMoreResult = NO;
        BOOL isSelect = NO;
        //批处理
        for (NSInteger i=0; i<self.monitorViewModel.monitorArray.count; i++) {
            MonitorModel *model= self.monitorViewModel.monitorArray[i];
            if (model.isSelect) {
                isSelect=YES;
            }
        }
        if (isSelect) {
            //批处理
            NSMutableArray *selectArray =[[NSMutableArray alloc]init];
            for (MonitorModel *model in self.monitorViewModel.monitorArray) {
                if (model.isSelect) {
                    [selectArray addObject:model];
                }
            }
            
            
            MoreResultViewController *moreResultController = [[MoreResultViewController alloc]initWithNibName:@"MoreResultViewController" bundle:nil];
            
            moreResultController.selectItemArray = selectArray;
            moreResultController.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:moreResultController animated:YES];
            
        } else {
            self.isShowMoreResult = NO;
            self.moreResultButton.titleLabel.text=@"选择处理";
            [self.tableView reloadData];
        }
        
        
    } else {
        //显示批处理功能
        self.isShowMoreResult = YES;
        self.moreResultButton.titleLabel.text=@"选择处理";
        [self.tableView reloadData];
    }
}

- (void)showTimeSelect:(BOOL)isStart {
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay CompleteBlock:^(NSDate *selectDate) {
        
        NSString *date = [selectDate stringWithFormat:@"yyyyMMdd"];
        NSLog(@"选择的日期：%@",date);
        
        if (isStart) {
            self.startTimeLabel.text = date;
        }else {
            self.endTimeLabel.text = date;
        }
    }];
    datepicker.dateLabelColor = RGB(65, 188, 241);//年-月-日-时-分 颜色
    datepicker.datePickerColor = [UIColor blackColor];//滚轮日期颜色
    datepicker.doneButtonColor = RGB(65, 188, 241);//确定按钮的颜色
    datepicker.yearLabelColor = [UIColor clearColor];//大号年份字体颜色
    [datepicker show];
}



/**
 切换选项
 
 @param sender seg
 */
- (IBAction)segmentChanged:(UISegmentedControl *)sender
{
    [self.historySearchBar resignFirstResponder];
    switch (sender.selectedSegmentIndex) {
        case 0:
            self.type = MonitorTypeReal;
            self.historyView.hidden = YES;
            self.mydeviceSegmentView.hidden = YES;
            [self.monitorViewModel getQueryRealTimeWarning:nil];
            self.historyConstraintHeight.constant = 0;
            self.mydeviceConstraintHeight.constant = 0;
            self.tableViewTopConstraintHeight.constant = 0;
            self.moreResultButton.hidden = NO;
            [self.tableView  reloadData];
            break;
            
        case 1:
            self.moreResultButton.hidden = YES;
            self.type = MonitorTypeHistory;
            self.historyView.hidden = NO;
            self.mydeviceSegmentView.hidden = YES;
            self.historySegmentView.hidden = NO;
            self.historyConstraintHeight.constant = 25;
            self.mydeviceConstraintHeight.constant = 0;
            self.tableViewTopConstraintHeight.constant = 25;
            [self.tableView  reloadData];
            break;
            
        case 2:
            self.moreResultButton.hidden = YES;
            self.type = MonitorTypeDevice;
            self.historyView.hidden = YES;
            self.mydeviceSegmentView.hidden = NO;
            self.historySegmentView.hidden = YES;
            self.historyConstraintHeight.constant = 0;
            self.mydeviceConstraintHeight.constant = 25;
            self.tableViewTopConstraintHeight.constant = 25;
            
            [self.monitorViewModel getAllDeviceInfo:@{@"pageNo" : @"1", @"pageSize" : @"500"}];
            [self.tableView  reloadData];
            break;
            
        default:
            break;
    }
    
}

- (IBAction)searchButClicked:(UIButton *)sender
{
    [self.historySearchBar resignFirstResponder];
    NSString *searchContent = self.historySearchBar.text;
    switch (sender.tag) {
        case 5000:
            //开始
            [self showTimeSelect:YES];
            break;
            
        case 5001:
            //结束
            [self showTimeSelect:NO];
            
            break;
            
        case 5002:{
            //搜索
            QueryHistoryViewController *historyController =kVCFromSB(kHistoryStoryBoard, QueryHistoryViewController);
            historyController.hidesBottomBarWhenPushed = YES;
            historyController.monitorViewModel=self.monitorViewModel;
            historyController.startTime=self.startTimeLabel.text;
            historyController.endTime=self.endTimeLabel.text;
            historyController.searchContent = searchContent;
            historyController.selectedHistoryIndex=self.selectedHistoryIndex;
            
            [self.navigationController pushViewController:historyController animated:YES];
            //
            //            self.historyView.hidden = YES;
            //            self.selectedHistoryIndex = 0;
            //            [self.monitorViewModel  getQueryHistoryWarning:@{@"start_date" : self.startTimeLabel.text, @"end_date" : self.endTimeLabel.text, @"type" : self.selectedHistoryIndex == 0 ? @"报警" : @"故障",@"keywords":searchContent}];
        }
            break;
            
        case 5003:{
            //两天
            //            self.historyView.hidden = YES;
            //            self.startTimeLabel.text =   [[NSDate dateWithDaysBeforeNow:1] stringWithFormat:@"yyyyMMdd"];
            //            self.endTimeLabel.text =  [[NSDate date] stringWithFormat:@"yyyyMMdd"];
            //            self.selectedHistoryIndex = 0;
            //            [self.monitorViewModel  getQueryHistoryWarning:@{@"start_date" : self.startTimeLabel.text, @"end_date" : self.endTimeLabel.text, @"type" : self.selectedHistoryIndex == 0 ? @"报警" : @"故障"}];
            
            QueryHistoryViewController *historyController =kVCFromSB(kHistoryStoryBoard, QueryHistoryViewController);
            historyController.hidesBottomBarWhenPushed = YES;
            self.selectedHistoryIndex = 0;
            historyController.monitorViewModel=self.monitorViewModel;
            historyController.startTime=[[NSDate dateWithDaysBeforeNow:1] stringWithFormat:@"yyyyMMdd"];
            historyController.endTime=[[NSDate date] stringWithFormat:@"yyyyMMdd"];
            historyController.searchContent = searchContent;
            historyController.selectedHistoryIndex=self.selectedHistoryIndex;
            [self.navigationController pushViewController:historyController animated:YES];
            
            
        }
            break;
            
        case 5004:{
            //七天
            //            self.historyView.hidden = YES;
            //            self.selectedHistoryIndex = 0;
            //            self.startTimeLabel.text =   [[NSDate dateWithDaysBeforeNow:6] stringWithFormat:@"yyyyMMdd"];
            //            self.endTimeLabel.text =  [[NSDate date] stringWithFormat:@"yyyyMMdd"];
            //            [self.monitorViewModel  getQueryHistoryWarning:@{@"start_date" : self.startTimeLabel.text, @"end_date" : self.endTimeLabel.text, @"type" : self.selectedHistoryIndex == 0 ? @"报警" : @"故障"}];
            
            
            QueryHistoryViewController *historyController =kVCFromSB(kHistoryStoryBoard, QueryHistoryViewController);
            self.selectedHistoryIndex = 0;
            historyController.hidesBottomBarWhenPushed = YES;
            historyController.monitorViewModel=self.monitorViewModel;
            historyController.startTime=[[NSDate dateWithDaysBeforeNow:6] stringWithFormat:@"yyyyMMdd"];
            historyController.endTime=[[NSDate date] stringWithFormat:@"yyyyMMdd"];
            historyController.searchContent = searchContent;
            historyController.selectedHistoryIndex=self.selectedHistoryIndex;
            [self.navigationController pushViewController:historyController animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.historySearchBar resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (self.type) {
        case MonitorTypeReal:
            return 137.5f;
            break;
            
        case MonitorTypeHistory:
            return 158.0f;
            break;
            
        default:
            break;
    }
    
    
    return 271.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.type == MonitorTypeDevice) {
        return self.newDeviceArray.count;
    }
    return [self.monitorViewModel.monitorArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MonitorModel *model = (self.type == MonitorTypeDevice) ? self.newDeviceArray[indexPath.row] : self.monitorViewModel.monitorArray[indexPath.row];
    
    switch (self.type) {
        case MonitorTypeReal:
        {
            //            MonitorRealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorRealTableViewCell"];
            //            cell.isShowMoreResult = self.isShowMoreResult;
            //            [cell setMonitorModel:model];
            
            
            
            
            MonitorItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorItemTableViewCell"];
            
            cell.isShowMoreResult = self.isShowMoreResult;
            cell.index = indexPath.row;
            cell.selectCallBackBlock = ^(NSInteger index) {
                //取消
                model.isSelect =YES;
            };
            cell.canCalclelBackBlock = ^(NSInteger index) {
                //选择
                model.isSelect =NO;
            };
            [cell setMonitorModel:model];
            
            return cell;
        }
            break;
        case MonitorTypeHistory:
        {
            MonitorRealTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorRealTableViewCell2"];
            [cell setMonitorModel:model];
            
            return cell;
        }
            break;
            
        case MonitorTypeDevice:
        {
            
            DeviceItemTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"DeviceItemTableViewCell"];
            
            //DeviceManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeviceManagerTableViewCell"];
            [cell setMonitorModel:model];
            
            return cell;
        }
            break;
        default:
            break;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    DeviceDetailInfoViewController *deviceVC = kVCFromSB(kMonitorStoryBoard, DeviceDetailInfoViewController);
    
    //    MonitorModel *model = self.monitorViewModel.monitorArray[indexPath.row];
    MonitorModel *model = (self.type == MonitorTypeDevice) ? self.newDeviceArray[indexPath.row] : self.monitorViewModel.monitorArray[indexPath.row];
    
    //deviceVC.isMisinformation = model.misinformation;
    NSString *str = [NSString stringWithFormat:@"%ld",model.timeHistory];
    NSString *breaktime = [NSString timeWithTimeIntervalString:str];
    
    
    if (self.type == MonitorTypeDevice) {
        //我的设备 设备详情
        DeviceDetailViewController2 *deviceVC =[[DeviceDetailViewController2 alloc]init];
        deviceVC.model=model;
        deviceVC.itemsArray = [NSArray array];
        deviceVC.title = @"设备详情";
        deviceVC.type =2;
        deviceVC.itemsArray = @[@[[NSString stringWithFormat:@"设备类型：%@", model.deviceType1], [NSString stringWithFormat:@"设备编号：%ld", model.deviceCode1], [NSString stringWithFormat:@"设备名称：%@", model.deviceName1], [NSString stringWithFormat:@"设备所在单位：%@", model.deviceOrg], [NSString stringWithFormat:@"设备所在地址：%@", model.deviceAddress1]], @[ [NSString stringWithFormat:@"经度：%@", model.latitude],[NSString stringWithFormat:@"纬度：%@", model.longitude]],@[[NSString stringWithFormat:@"探测器数量：%ld", model.detectorAmount1], [NSString stringWithFormat:@"控制器型号：%@", model.controllerSpec1], [NSString stringWithFormat:@"控制器厂家：%@", model.controllerManufacturer1], [NSString stringWithFormat:@"产品编号：%@", model.itemCode1], [NSString stringWithFormat:@"采购时间：%@", model.purchaseTime1]],@[[NSString stringWithFormat:@"售后电话：%@", model.hotline], [NSString stringWithFormat:@"维护联系人：%@", model.linkMan1], [NSString stringWithFormat:@"维保年限：%@", model.wbnx], [NSString stringWithFormat:@"维护联系人电话：%@", model.phone]],@[[NSString stringWithFormat:@"备注：%@", model.remark]]];
        
        deviceVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:deviceVC animated:YES];
    }else if(self.type == MonitorTypeReal){
        //实时信息 设备详情
        HistoryDetaileViewController *contryll=[[HistoryDetaileViewController alloc]init];
        contryll.monitorModel = model;
        contryll.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:contryll animated:YES];
    }else{
        
    }
    
    //    if (self.type != MonitorTypeDevice) {
    //        deviceVC.title = @"信息详情";
    //        deviceVC.type =1;
    //
    //        deviceVC.itemsArray = @[@[[NSString stringWithFormat:@"设备名称：%@", model.deviceName], [NSString stringWithFormat:@"设备编号：%ld", model.deviceCode], [NSString stringWithFormat:@"时间：%@", breaktime], [NSString stringWithFormat:@"来源：%@", model.record1], [NSString stringWithFormat:@"类型：%@", model.record2], [NSString stringWithFormat:@"内容：%@", model.record3]], @[[NSString stringWithFormat:@"设备信息：%@", model.deviceName], [NSString stringWithFormat:@"设备类型：%@", model.deviceType], [NSString stringWithFormat:@"控制器厂商：%@", model.controllerManufacturer], [NSString stringWithFormat:@"控制器型号：%@", model.controllerSpec], [NSString stringWithFormat:@"产品编号：%@", model.itemCode], [NSString stringWithFormat:@"探测器数量：%ld", model.detectorAmount1], [NSString stringWithFormat:@"采购时间：%@", model.purchaseTime], [NSString stringWithFormat:@"设备所在地：%@", model.deviceAddress], [NSString stringWithFormat:@"维保年限：%@", model.wbnx], [NSString stringWithFormat:@"维护联系人：%@", model.linkMan], [NSString stringWithFormat:@"联系人电话：%@", model.phone]], @[[NSString stringWithFormat:@"经度：%@", model.longitude], [NSString stringWithFormat:@"纬度：%@", model.latitude]], @[[NSString stringWithFormat:@"联系电话：%@", model.phone]]];
    //
    //
    //    } else {
    //
    //    }
    
    //    deviceVC.itemsArray = @[@[[NSString stringWithFormat:@"设备名称：%@", model.deviceName], [NSString stringWithFormat:@"设备编号：%ld", model.deviceCode], [NSString stringWithFormat:@"时间：%@", breaktime], [NSString stringWithFormat:@"来源：%@", model.record1], [NSString stringWithFormat:@"类型：%@", model.record2], [NSString stringWithFormat:@"内容：%@", model.record3]], @[[NSString stringWithFormat:@"设备信息：%@", model.deviceName], [NSString stringWithFormat:@"设备类型：%@", model.deviceType], [NSString stringWithFormat:@"控制器厂商：%@", model.controllerManufacturer], [NSString stringWithFormat:@"控制器型号：%@", model.controllerSpec], [NSString stringWithFormat:@"产品编号：%@", model.itemCode], [NSString stringWithFormat:@"探测器数量：%ld", model.detectorAmount], [NSString stringWithFormat:@"采购时间：%@", model.purchaseTime], [NSString stringWithFormat:@"设备所在地：%@", model.deviceAddress], [NSString stringWithFormat:@"维保年限：%@", model.wbnx], [NSString stringWithFormat:@"维护联系人：%@", model.linkMan], [NSString stringWithFormat:@"联系人电话：%@", model.phone]], @[[NSString stringWithFormat:@"经度：%@", model.longitude], [NSString stringWithFormat:@"纬度：%@", model.latitude]], @[[NSString stringWithFormat:@"联系电话：%@", model.phone]]];
    
    
}

- (MonitorViewModel *)monitorViewModel
{
    if (_monitorViewModel == nil) {
        _monitorViewModel = [[MonitorViewModel alloc] init];
    }
    
    return _monitorViewModel;
}

- (DeviceViewModel *)deviceViewModel
{
    if (_deviceViewModel == nil) {
        _deviceViewModel = [[DeviceViewModel alloc] init];
    }
    
    return _deviceViewModel;
}

- (NSMutableArray *)newDeviceArray
{
    if (_newDeviceArray == nil) {
        _newDeviceArray = [NSMutableArray array];
    }
    
    return _newDeviceArray;
}

- (NSMutableArray *)devicesArray
{
    if (_devicesArray == nil) {
        _devicesArray = [NSMutableArray array];
    }
    
    return _devicesArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
    
    
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

/**
 * 获得未读数
 */
- (void)setupUnreadCount:(NSInteger) count
{
    BOOL isShow =[[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_ISMINKD] boolValue];
    if (isShow) {
        // 设置提醒数字(微博的未读数)
        NSString *status = [NSString stringWithFormat:@"%ld", count];
        if ([status isEqualToString:@"0"]) { // 如果是0，得清空数字
            self.tabBarItem.badgeValue = nil;//应用图标显示
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
        } else { // 非0情况
            self.tabBarItem.badgeValue = status;
            [UIApplication sharedApplication].applicationIconBadgeNumber = status.intValue;
        }
    }else{
        self.tabBarItem.badgeValue = nil;//应用图标显示
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
    
}

@end
